__author__ = 'amin'
import cairo
import math
class DrawCanvas():
    def __init__(self, features, scores, w, h):
        l = len(features)
        self.range = features[0].start
        self.end = features[0].width
        self.set_scale = 10000
        self.w = w
        self.h = h
        self.xoffset = 100
        self.scores = scores
        self.scale = 200

        self.font = "Ubuntu"
        self.features = features
        self.s = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
        self.c = cairo.Context(self.s)
        self.c.set_line_width(.1)
        self.c.set_antialias(cairo.ANTIALIAS_SUBPIXEL)


        self.c.rectangle(0,0,w,h)
        self.c.set_source_rgba(1,1,1,1)
        self.c.fill()



        #for i in range(int(w/ 100)):
        #    self.c.set_line_width(.2)
        #    self.c.set_source_rgba(0, 0, .9,.5)
        #    self.c.set_dash([2],1)
        #    self.c.move_to(i * 100,0)
        #    self.c.line_to(i * 100,h)
        #    self.c.stroke()


        self.c.set_line_width(.5)
        self.c.set_dash((),0)
        self.c.set_source_rgba(0, 0, 0,1)
        self.c.rectangle(self.xoffset, 50, w, 2)
        self.c.fill()

        #self.c.move_to(20,10)
        #self.c.line_to(20,20)
        #self.c.move_to(20,15)
        #self.c.line_to(40,15)
        #self.c.move_to(40,10)
        #self.c.line_to(40,20)
        #self.c.stroke()
        self.c.select_font_face(self.font, cairo.FONT_SLANT_NORMAL,
        cairo.FONT_WEIGHT_NORMAL)
        self.c.set_font_size(10)
        self.c.move_to(20,8)
        self.c.show_text(str(self.set_scale) + " b")


        for i in features:
            self.drawfeature(i)
        if self.scores is not None:
            for i in range(len(self.scores)):
                self.drawscores(self.scores[i][1],i+1, self.scores[i][0])




        self.s.write_to_png("output.png")

    def drawfeature(self,f):

        if f.strand == "p":
            f.size = int(f.width) - int(f.start)

            scale = self.scale

            f.start -= self.range
            f.start /= self.scale
            f.width =   f.size / scale

        else:
            f.size = int(f.width) - int(f.start)
            scale = self.scale
            f.start -= self.range
            f.start /= self.scale

            f.width = -1* f.size / scale

        print "Drawing: " + f.name


        f.start += self.xoffset
        if f.type=="gene":
            self.drawgene(f)
        elif f.type=="motif":
            self.drawmotif(f)
        elif f.type=="cpg":
            self.drawcpg(f)
        elif f.type=="ccc":
            self.drawccc(f)

    def drawscores(self,data_source,item, name):
        print "Drawing: Scores"
        self.c.set_source_rgb(0,0,0)
        self.c.select_font_face(self.font, cairo.FONT_SLANT_NORMAL,
        cairo.FONT_WEIGHT_NORMAL)
        self.c.set_font_size(10)
        self.c.move_to(20,160 * item +1)
        self.c.show_text(name)
        j = 0
        for i in range(self.range, self.end):

            if j > self.set_scale or j==0 and item==0:
                self.c.set_source_rgba(0, 0, 0,.6)
                self.c.rectangle(self.xoffset + ((float(i)-self.range)/self.scale) , 0 , .1, self.h)
                self.c.fill()
                j = 0
            j += 1

            try:

                if float(data_source[i]) <= 0:
                    self.c.set_source_rgb(0, 0, 1)
                else:
                    self.c.set_source_rgb(1, 0, 0)

                self.c.rectangle(self.xoffset + ((i-self.range)/self.scale) , 160 * item + 1 , .1, (data_source[i]*4))
                self.c.fill()
            except:
                pass


    def drawccc(self, f):
        self.c.set_source_rgb(0, 0, 0)
        self.c.set_dash([2],1)
        self.c.move_to(float(f.start), 50)
        self.c.curve_to(float(f.start), 50, (float(f.start)+float(f.width))/2, -30, float(f.width), 50)
        self.c.set_line_width(1)
        self.c.stroke()
        self.c.set_dash([],0)


    def drawcpg(self, f):
        if f.strand == "yes":
            self.c.set_source_rgb(0, 0, 0)
            self.c.rectangle(float(f.start), 50,1, -15)
            self.c.fill()
            self.c.arc(float(f.start), 50-18, 4, 0, 2*math.pi)
            self.c.fill()
        else:
            self.c.set_source_rgb(0, 0, 0)
            self.c.rectangle(float(f.start), 50,1, -15)
            self.c.fill()
            self.c.arc(float(f.start), 50-18, 4, 0, 2*math.pi)
            self.c.stroke()



    def drawmotif(self, f):
        if f.strand == "n":
            self.c.set_source_rgb(1, 1, 1)
            self.c.rectangle(float(f.start), 50-5,float( f.width) * -1, 12)
            self.c.fill()

            self.c.set_source_rgb(0, 0, 0)
            self.c.rectangle(float(f.start), 50-5,float( f.width) * -1, 12)
            self.c.stroke()



            self.c.select_font_face(self.font, cairo.FONT_SLANT_NORMAL,
        cairo.FONT_WEIGHT_NORMAL)
            self.c.set_font_size(13)
            self.c.move_to(float(f.start)-len(f.name)-20, 50 -9)
            self.c.show_text(f.name)

        else:
            self.c.set_source_rgb(1, 1, 1)
            self.c.rectangle(float(f.start), 50-5,float( f.width), 12)
            self.c.fill()


            self.c.set_source_rgb(0, 0, 0)
            self.c.rectangle(float(f.start), 50-5,float( f.width), 12)
            self.c.stroke()


            self.c.select_font_face(self.font, cairo.FONT_SLANT_NORMAL,
        cairo.FONT_WEIGHT_NORMAL)
            self.c.set_font_size(13)
            self.c.move_to(float(f.start)+len(f.name), 50 +29)
            self.c.show_text(f.name)



    def drawgene(self, f):
        if f.strand == "n":
            self.c.set_source_rgb(0, 0, 0)
            self.c.rectangle(float(f.start), 50-5,float( f.width) * -1, 12)
            self.c.fill()
            self.c.rectangle(float(f.start), 50-5, 2, 30)
            self.c.fill()
            self.c.rectangle(float(f.start)+2, 50-5+30  , -10, 2)
            self.c.fill()
            self.c.move_to(float(f.start)-5, 50-5+23)
            self.c.line_to(float(f.start)-5, 50-5+37)
            self.c.line_to(float(f.start)-20, 50-5+30)
            self.c.fill()
            self.c.select_font_face(self.font, cairo.FONT_SLANT_NORMAL,
        cairo.FONT_WEIGHT_NORMAL)
            self.c.set_font_size(13)
            self.c.move_to(float(f.start)-len(f.name)*8, 50 -9)
            self.c.show_text(f.name)

        else:
            self.c.set_source_rgb(0, 0, 0)
            self.c.rectangle(float(f.start), 50-5,float( f.width), 12)
            self.c.fill()
            self.c.rectangle(float(f.start), 50-5,2, -30)
            self.c.fill()
            self.c.rectangle(float(f.start)+2, 50-5-30,10, 2)
            self.c.fill()
            self.c.move_to(float(f.start)+5, 50-5-23)
            self.c.line_to(float(f.start)+5, 50-5-35)
            self.c.line_to(float(f.start)+20, 50-5-30)
            self.c.fill()
            self.c.select_font_face(self.font, cairo.FONT_SLANT_NORMAL,
        cairo.FONT_WEIGHT_NORMAL)
            self.c.set_font_size(13)
            self.c.move_to(float(f.start)+len(f.name), 50 +19)
            self.c.show_text(f.name)




